import cv2
import numpy as np

def get_hsv_color_range(image_path):
    # 读取图片
    image = cv2.imread(image_path)
    
    if image is None:
        print("Failed to read the image.")
        return
    
    # 将图片转换为 HSV 颜色空间
    hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
    
    # 计算 HSV 颜色空间中的最小值和最大值
    h_min, s_min, v_min = np.min(hsv_image, axis=(0, 1))
    h_max, s_max, v_max = np.max(hsv_image, axis=(0, 1))
    
    return (h_min, s_min, v_min), (h_max, s_max, v_max)

# 指定PNG图片路径
image_path = "/home/chf/python_obj/CWD/test.png"

# 获取 HSV 颜色范围
hsv_min, hsv_max = get_hsv_color_range(image_path)
print("HSV Color Range:")
print("Min: ", hsv_min)
print("Max: ", hsv_max)

"""
# 背景颜色
Min:  (60, 1, 191)
Max:  (60, 1, 191)

# blue
Min:  (119, 238, 207)
Max:  (120, 248, 222)

# green
Min:  (58, 190, 121)
Max:  (62, 233, 133)

# red
Min:  (2, 125, 253)
Max:  (5, 140, 255)

# red-single
Min:  (0, 224, 255)
Max:  (179, 244, 255)

# white
Min:  (0, 0, 249)
Max:  (165, 8, 255)

yellow
Min:  (28, 37, 245)
Max:  (33, 63, 255)

# cwd 灰色背景
Min:  (38, 4, 166)
Max:  (45, 6, 201)
"""